package cn.jsj.machine.dao;

import cn.jsj.machine.pojo.CPart;
import org.apache.ibatis.annotations.*;
import org.hibernate.validator.constraints.Length;
import org.springframework.stereotype.Repository;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@Repository
public interface IPartDao {

    @Results(id = "toPart",value = {
            @Result(id = true,property = "partID",column = "part_id"),
            @Result(property = "partName",column = "part_name"),
            @Result(property = "remaining",column = "remaining"),
            @Result(property = "waring_num",column = "waring_num"),
            @Result(property = "rmb", column = "rmb")
    })
    @Select("select * from tb_parts")
    public List<CPart> getAllparts();

    @Update("update tb_parts set rmb = #{rmb} where part_id = #{partID}")
    public Integer resetRmbOfPart(@Param("partID")@NotNull@Min(value = 1) Integer partID,@Param("rmb")@NotNull@Min(value = 0)@Max(value = 999999999) Double rmb);

    @Update("update tb_parts set remaining = remaining + #{addNumber} where part_id = #{partID}")
    public Integer addNumberOfPart(@Param("partID")@NotNull@Min(value = 1) Integer partID,@Param("addNumber")@NotNull@Min(value = 0) Integer addNumber);

    @Update("update tb_parts set remaining = remaining - #{cutNumber} where part_id = #{partID}")
    public Integer cutNumberOfPart(@Param("partID")@NotNull@Min(value = 1) Integer partID,@Param("cutNumber")@NotNull@Min(value = 0) Integer cutNumber);

    @ResultMap(value = "toPart")
    @Select("select * from tb_parts where part_id = #{partID} limit 1")
    public CPart getOnePartByID(@Param("partID")@NotNull@Min(value = 1) Integer partID);


    @Update("update tb_parts set waring_num = #{wanningValue} where part_id = #{partID}")
    public Integer resetWaringValueOfPart(@Param("partID")@NotNull@Min(value = 1) Integer partID,@Param("wanningValue")@NotNull@Min(value = 1) Integer wanningValue);


    @ResultMap(value = "toPart")
    @Select("select * from tb_parts where part_name = #{partName}")
    public CPart getOnePartByName(@Param("partName")@NotBlank@Length(max = 20) String partName);

    @Results(id = "toPartWithNumber",value = {
            @Result(id = true,property = "partID",column = "part_id"),
            @Result(property = "partName",column = "part_name"),
            @Result(property = "remaining",column = "remaining"),
            @Result(property = "waring_num",column = "waring_num"),
            @Result(property = "rmb", column = "rmb"),
            @Result(property = "requreNumber", column = "part_number")
    })
    @Select("select tb_parts.*,tb_machine_part.part_number from tb_parts inner join tb_machine_part on tb_parts.part_id = tb_machine_part.part_id where tb_machine_part.machine_id = #{machineID}")
    public List<CPart> getPartListOfMachine(@Param("machineID") Integer machineID);

    @Delete("delete from tb_parts where part_id = #{partID}")
    public Integer deleteOnePart(@Param("partID")@NotNull@Min(value = 1) Integer partID);

    @Update("update tb_parts set part_name = #{newName} where part_id = #{partID}")
    public Integer renameOfPart(@Param("partID")@NotNull@Min(value = 1) Integer partID,@Param("newName")@NotBlank@Length(min = 1,max = 20) String newName);

    @Insert("insert into tb_parts (part_name, remaining, waring_num, rmb) values (#{partName}, #{remain}, #{waring}, #{rmb})")
    public Integer addOnepart(@Param("partName")@NotBlank String partName,@Param("remain")@NotNull@Min(value = 0) Integer remain,@Param("waring")@NotNull@Min(value = 0) Integer waring,@Param("rmb")@NotNull@Min(value = 0) Double rmb);
}
